package com.cgnpc.scp.common.service.impl;

import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.result.ApiResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.cuddemo.utils.IDHelper;
import com.cgnpc.scp.common.mybatis.entity.ScpCommonSmsInfo;
import com.cgnpc.scp.common.mybatis.mapper.client.ScpCommonSmsInfoMapper;
import com.cgnpc.scp.common.service.IScpCommonSmsInfoService;
import com.cgnpc.scp.common.utils.AjaxResultUtil;
import com.cgnpc.scp.common.utils.JacksonUtil;
import com.cgnpc.scp.common.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ScpCommonSmsInfoServiceImpl implements IScpCommonSmsInfoService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ScpCommonFileUploadServiceImpl.class);

    @Autowired
    private ScpCommonSmsInfoMapper scpCommonSmsInfoMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult save(ScpCommonSmsInfo... reqScpCommonSmsInfos) {

        try {
            if (reqScpCommonSmsInfos == null || reqScpCommonSmsInfos.length <= 0) {
                return AjaxResultUtil.createErrorMessage("保存失败");
            }

            for (ScpCommonSmsInfo reqScpCommonSmsInfo : reqScpCommonSmsInfos) {
                // 判断id是否存在
                if (StringUtil.isNullOrSpace(reqScpCommonSmsInfo.getId())) {
                    reqScpCommonSmsInfo.setId(IDHelper.uuidNew());
                }

                reqScpCommonSmsInfo.setResCode(ScpCommonSmsInfo.RESCODE_NEEDSEND);
                scpCommonSmsInfoMapper.insert(reqScpCommonSmsInfo);
            }
        } catch (Exception e) {
            LOGGER.error("save.error;", e);
            throw e;
        }

        return null;
    }

    @Override
    public List<ScpCommonSmsInfo> loadNeedSendSms(int pageNum, int pageSize) {
        // begin 分页查询
        // 创建Page对象
        IPage<ScpCommonSmsInfo> queryPage = new Page<>();
        queryPage.setCurrent(pageNum);
        queryPage.setSize(pageSize);

        // 创建queryExample
        QueryWrapper<ScpCommonSmsInfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.orderByDesc("id");

        // addCriterion("res_code =", value, "resCode");
        queryWrapper.eq("res_code", ScpCommonSmsInfo.RESCODE_NEEDSEND);

        IPage<ScpCommonSmsInfo> pageResult = scpCommonSmsInfoMapper.selectPage(queryPage, queryWrapper);

        return pageResult.getRecords();
        // end 分页查询
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSmsSend(String reqScpCommonSmsInfoId, ApiResult apiResult) {
        if (StringUtil.isNullOrSpace(reqScpCommonSmsInfoId) || apiResult == null) {
            return;
        }

        try {
            ScpCommonSmsInfo usedScpCommonSmsInfo = new ScpCommonSmsInfo();
            usedScpCommonSmsInfo.setResponseId(apiResult.getResponseId());
            usedScpCommonSmsInfo.setResCode(apiResult.getCode());
            usedScpCommonSmsInfo.setResMsg(apiResult.getMsg());
            usedScpCommonSmsInfo.setResData(JacksonUtil.writeValueAsString(apiResult.getData()));

            UpdateWrapper<ScpCommonSmsInfo> updateWrapper = new UpdateWrapper<>();

            updateWrapper.eq("id", reqScpCommonSmsInfoId);

            scpCommonSmsInfoMapper.update(usedScpCommonSmsInfo, updateWrapper);
        } catch (Exception e) {
            LOGGER.error("save.error;", e);
            throw e;
        }
    }
}
